package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.MineClassData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.utils.Utils;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerMineClassComponent;
import com.eduhzy.ttw.teacher.mvp.contract.MineClassContract;
import com.eduhzy.ttw.teacher.mvp.presenter.MineClassPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.zhy.autolayout.AutoRelativeLayout;

import org.simple.eventbus.Subscriber;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 */
@Route(path = RouterHub.APP_MINECLASSACTIVITY)
public class MineClassActivity extends BaseActivity<MineClassPresenter> implements MineClassContract.View {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.public_toolbar_right_iv_layout)
    AutoRelativeLayout add;
    @Inject
    BaseQuickAdapter<MineClassData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager layoutManager;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMineClassComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_mine_class; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        add.setVisibility(View.VISIBLE);
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        swipeLayout.setOnRefreshListener(() ->
                mPresenter.getClassByUser()
        );
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            ARouter.getInstance().build(RouterHub.APP_CLASSDETAILSACTIVITY)
                    .withString(Intent.EXTRA_TITLE, Constants.CLASS_DETAILS)
                    .withParcelable(Constants.ROUTER_DATA, mAdapter.getData().get(position))
                    .navigation();
        });
        mAdapter.setOnItemLongClickListener((adapter, view, position) -> {

            MineClassData data = mAdapter.getData().get(position);
            RxUtil.showDeleteDialog(getActivity(), (dialog, index) -> {
                switch (index) {
                    case 0:
                        dialog.dismiss();
                        break;
                    case 1:
                        if (!data.getBzrgh().equals(SPUtils.getInstance().getString(Constants.USERID))) {
                            showMessage("非班主任不能删除班级！");
                            return;
                        }
                        mPresenter.deleteClass(data.getId(), SPUtils.getInstance().getString(Constants.USERID), position);
                        dialog.dismiss();
                        break;
                    default:

                        break;
                }
            });
            return false;
        });
        mPresenter.getClassByUser();
    }

    @Override
    public void showLoading() {
        swipeLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        swipeLayout.setRefreshing(false);

    }

    @Subscriber(tag = RouterHub.APP_MINECLASSACTIVITY)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.CREATE_CLASS_SUCCESS:
                mPresenter.getClassByUser();
                break;
            case EventBusTags.DELETE_CLASS_SUCCESS:
                mPresenter.getClassByUser();
                break;
            case EventBusTags.TRANSFER_CLASS_SUCCESS:
                mPresenter.getClassByUser();
                break;
            case EventBusTags.UPDATE_CLASS_SUCCESS:
                mPresenter.getClassByUser();
                break;
            default:
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
//        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void showAdapterEmpty() {
        View view = View.inflate(getActivity(), R.layout.class_empty_view, null);
        view.findViewById(R.id.btn_join).setOnClickListener(view1 -> {
            ARouter.getInstance().build(RouterHub.APP_JOINCLASSACTIVITY)
                    .withString(Intent.EXTRA_TITLE, Constants.JOIN_CLASS)
                    .navigation();
        });
        view.findViewById(R.id.btn_create).setOnClickListener(view2 -> {
            Utils.navigationWithTitle(RouterHub.APP_CREATECLASSACTIVITY, Constants.CREATE_CLASS);
        });
        mAdapter.setEmptyView(view);
    }

    @OnClick(R.id.public_toolbar_right_iv_layout)
    public void onViewClicked() {
        Utils.navigationWithTitle(RouterHub.APP_CREATECLASSACTIVITY, Constants.CREATE_CLASS);
    }
}
